package com.youdianstar.app.ui.help

import android.Manifest
import android.app.Dialog
import com.youdianstar.app.BR
import com.youdianstar.app.R
import com.youdianstar.app.databinding.ActivityFaqDetailsBinding
import com.youdianstar.app.entity.FaqQuestionItem
import com.youdianstar.app.util.AppDialogUtil
import com.youdianstar.app.common.ConfigKey
import com.youdianstar.app.common.Extra
import com.youdianstar.app.common.base.BaseActivity
import com.youdianstar.app.common.ex.getObject
import com.youdianstar.app.common.ex.setThrottleListener
import com.youdianstar.app.common.util.Tool

import com.youdianstar.app.common.util.dialog.DialogUtil
import com.youdianstar.app.util.DataAppManager
import com.youdianstar.app.common.auko.startActivity
import org.koin.androidx.viewmodel.ext.android.viewModel


/**
 * File: FaqDetailActivity
 * Author: 82149 Create: 2021/12/1 12:05
 * Changes (from 2021/12/1)
 * --------------------------------------------------
 * description:
 * ---------------------------------------------------
 *
 */
class FaqDetailActivity :BaseActivity<ActivityFaqDetailsBinding,FaqViewModel>(){
    override fun layoutId(): Int = R.layout.activity_faq_details

    override fun initVariableId(): Int = BR.viewModel

    override fun initViewModel(): FaqViewModel {
        val model : FaqViewModel by viewModel()
        return model
    }

    private var question :FaqQuestionItem?=null
    private var ctDialog : Dialog?=null
    private var whatsNum : String?=null
    private var phoneNum : String?=null
    private var emailStr : String?=null
    private var wechatLink : String?=null
//    private var zaloLink : String?=null

    override fun initData() {
        super.initData()
        mBinding.headBar.initTitle(this,getString(R.string.menu_helpCentre), showInner = true)

        question = intent.getObject(Extra.IT_DATA)
        question?.let {
            mBinding.tvTitle.text = it.title
            mBinding.tvContent.text = it.content
        }
        whatsNum = DataAppManager.instance.getConfig(ConfigKey.WhatsApp)
        phoneNum = DataAppManager.instance.getConfig(ConfigKey.PHONE)
        emailStr = DataAppManager.instance.getConfig(ConfigKey.EMAIL)
        wechatLink = DataAppManager.instance.getConfig(ConfigKey.WechatQrLink)
//        zaloLink = DataAppManager.instance.getConfig(ConfigKey.ZaloQrLink)
    }

    override fun initClickEvent() {
        mBinding.ivCus.setThrottleListener {
            showContactDialog()
        }
        mBinding.btnFeedback.setThrottleListener {
            //弃用
            startActivity<FeedbackActivity>()
        }
        mBinding.btnSupport.setThrottleListener {
            //弃用
            showContactDialog()
        }
    }

    //----------------------------------------------------------------
    override fun onPermissionsGranted(requestCode: Int, perms: List<String>) {
        super.onPermissionsGranted(requestCode, perms)
        if (requestCode == REQUEST_PERMISSION_CODE)
            callPhone()
    }

    private fun requestCallPermission() {
        phoneNum.let { phone ->
            val phoneCall = String.format(getString(R.string.Dialog_SelectCall_Message),phone)
            showActionDialog(phoneCall) {
                requestPermission(
                    getString(R.string.Call_Permission_Tip),
                    Manifest.permission.CALL_PHONE,
                    granted = {
                        callPhone()
                    })
            }
        }
    }

    private fun callPhone(){
//        phoneNum?.let { phone ->
//            Tool.callPhone(this,phone)
//        }
    }

    private fun callEmail(){
        emailStr?.let { email ->
            val emailCall = String.format(getString(R.string.Dialog_SelectEmail_Message),email)
            showActionDialog(emailCall) {
                Tool.callEmail(this,email)
            }
        }
    }

    private fun callWhatsapp(){
        whatsNum?.let { num ->
            val appCall = getString(R.string.Dialog_SelectWhatsapp_Message)
            showActionDialog(appCall) {
                Tool.callWhatsApp(this,num)
            }
        }
    }

    private fun showContactDialog(){
        ctDialog = AppDialogUtil.instance.showContactDialog(this,
            phoneStr = whatsNum, emailStr = emailStr, wechatLink = wechatLink,
            callAction = {
            callWhatsapp()
        },emailAction = {
            callEmail()
        })
    }

    private fun showActionDialog(msg :String, confirmAction: (() -> Unit) = { }){
        DialogUtil.instance.showNotifyDialog(this,
            title = getString(R.string.public_notice),
            msg = msg,leftAction = {

            },left = getString(R.string.public_cancel),
            rightAction = confirmAction,
            right = getString(R.string.profile_confirm),
            isShowLeft = true,isShowRight = true)
    }

    override fun onDestroy() {
        ctDialog?.let {
            if (it.isShowing)
                it.dismiss()
        }
        super.onDestroy()
    }
}